/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __frmodule_cfg_params2str.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/03 by geyh                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <dlfcn.h>
#include <sys/prctl.h>

#include "frcomm.h"
#include "frmem.h"
#include "apmtest_cunit.h"
#include "frlist.h"

#include "conntrack.h"
#include "frmodule_mng.h"
#include "frct_comm.h"

#include "frxml_parse.h"
#include "frversion.h"
#include "frvar_field.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

extern char g_arr_frame_version[30];
extern void *g_p_memstat;
extern int g_i_flag_run;

extern Frlist_header *g_p_st_hdr_mng_module_del;
extern Frlist_header *g_p_st_hdr_mng_module_use;
void __list_node_mng_free(Frlist_node *p_st_node);

void __frmodule_cfg_params2str(Frlist_header *p_lst_hdr, const char *p_header, char **pp_outbuf);

static int test_my_create(void)
{
    g_p_memstat = fr_slab_init2(5242880, "framework");
    
    //初始化
    g_p_st_hdr_mng_module_use = frlist_create();
    if(g_p_st_hdr_mng_module_use == NULL)
        return FR_FAIL;
    
    g_p_st_hdr_mng_module_del = frlist_create();
    if(g_p_st_hdr_mng_module_del == NULL)
    {
        frfree(g_p_st_hdr_mng_module_use);
        return FR_FAIL;
    }
    
    return FR_SUCC;
}

static void test_my_destroy(void)
{
    if(g_p_st_hdr_mng_module_del)
    {
        frlist_destroy(g_p_st_hdr_mng_module_del, __list_node_mng_free);
        g_p_st_hdr_mng_module_del = NULL;
    }
    
    if(g_p_st_hdr_mng_module_use)
    {
        frlist_destroy(g_p_st_hdr_mng_module_use, __list_node_mng_free);
        g_p_st_hdr_mng_module_use = NULL;
    }
    
    fr_slab_destroy();
    return;
}


static void __add_field(Frlist_header *p_lst_hdr, const char *p_key, const char *p_value)
{
    Frvar_field *p_var_field = NULL;
    Frlist_node *p_st_node = NULL;

    p_var_field = frvarfield_str(p_key, p_value);

    p_st_node = frlist_node_create (p_var_field);

    frlist_push (p_lst_hdr, p_st_node);

    return ;

}

static void case1(void)
{
    Frlist_header *p_lst_hdr = NULL;
    char *p_outbuf = NULL;

    p_lst_hdr = frlist_create ();

    __add_field(p_lst_hdr, "key1", "value1");
    __add_field(p_lst_hdr, "key2", "value2");
    __add_field(p_lst_hdr, "key3", "value3");

    __frmodule_cfg_params2str(p_lst_hdr, NULL, &p_outbuf);

    CU_ASSERT_EQUAL(memcmp(p_outbuf, "value1\0value2\0value3\0", 21), 0);

    frfree (p_outbuf);

    return;
}

static void case2(void)
{
    Frlist_header *p_lst_hdr = NULL;
    char *p_outbuf = NULL;

    p_lst_hdr = frlist_create ();

    __add_field(p_lst_hdr, "key1", "value1");
    __add_field(p_lst_hdr, "key2", "value2");
    __add_field(p_lst_hdr, "key3", "value3");

    __frmodule_cfg_params2str(p_lst_hdr, "test", &p_outbuf);

    CU_ASSERT_EQUAL(memcmp(p_outbuf, "test\0 \0value1\0 \0value2\0 \0value3\0", 32), 0);

    frfree (p_outbuf);

    return;
}

static void case3(void)
{
    Frlist_header *p_lst_hdr = NULL;
    char *p_outbuf = NULL;

    p_lst_hdr = frlist_create ();

    __add_field(p_lst_hdr, "key1", "value1");
    __add_field(p_lst_hdr, "key2", "value2");
    __add_field(p_lst_hdr, "key3", "value3");

    __frmodule_cfg_params2str(p_lst_hdr, "test", NULL);

    printf ("pout = %s\n", p_outbuf);
    return;

}

int add_suite()
{
    CU_pSuite pSuite = NULL;
    
    pSuite = CU_add_suite("__frmodule_cfg_params2str", __suite_init, __suite_cleanup);
    
    if (NULL == pSuite)
    {
        return 1;
    }
    
    //增加测试用例
    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
//    CU_add_test(pSuite, "case4", case4);
//    CU_add_test(pSuite, "case5", case5);
//    CU_add_test(pSuite, "case6", case6);
//    CU_add_test(pSuite, "case7", case7);
//    CU_add_test(pSuite, "case8", case8);
//    CU_add_test(pSuite, "case9", case9);
//    CU_add_test(pSuite, "case10", case10);
    
    return 0;
}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    
    xmlInitParser();
    frversion_set(g_arr_frame_version, sizeof(g_arr_frame_version), 0);
    
    run_cunit(add_suite);
    
    xmlCleanupParser();
    fr_slab_destroy();
    
    return 0;
}


